package com.chushouya.manager.dao.repository;


import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Longs;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.PartnerEntity;
import com.chushouya.manager.dao.mapper.PartnerEntityMapper;
import com.chushouya.manager.dto.admin.partner.PartnerQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class PartnerRepository extends BaseRepository<PartnerEntityMapper, PartnerEntity, Long> {

    public PageResult<PartnerEntity> selectPageResult(PartnerQuery query) {
        final Example example = this.buildExample(query);
        example.orderBy("createTime").desc();
        return this.selectPage(query.getPageRequest(), example);
    }

    private Example buildExample(PartnerQuery query) {
        Example example = buildExample();
        if(Longs.notNullAndZero(query.getPartnerId())){
            example.and().andEqualTo("partnerId",query.getPartnerId());
        }
        if (Strings.isNotBlank(query.getPartnerName())) {
            example.and().andLike("partnerName", Strings.format("%{}%", query.getPartnerName()));
        }
        if (Strings.isNotBlank(query.getRemark())) {
            example.and().andLike("remark", Strings.format("%{}%", query.getRemark()));
        }
        if (Objects.nonNull(query.getStatus())) {
            example.and().andEqualTo("status", query.getStatus());
        }
        if (Objects.nonNull(query.getPartnerType())) {
            example.and().andEqualTo("partnerType", query.getPartnerType());
        }
        if (Objects.nonNull(query.getAdminUserId())) {
            example.and().andEqualTo("adminUserId", query.getAdminUserId());
        }
        if (Lists.isNotEmpty(query.getPartnerTypeList())) {
            example.and().andIn("partnerType", query.getPartnerTypeList());
        }
        if (Lists.isNotEmpty(query.getPartnerIdList())) {
            example.and().andIn("partnerId", query.getPartnerIdList());
        }

        return example;
    }

    public List<PartnerEntity> selectList(PartnerQuery query) {
        final Example example = this.buildExample(query);
        example.orderBy("createTime").asc();
        return this.selectByExample(example);
    }

    public List<PartnerEntity> selectPageList(PartnerQuery query) {
        final Example example = this.buildExample(query);
        example.orderBy("createTime").desc();
        return this.selectPageList(query.getPageRequest(), example);
    }

    public PartnerEntity selectByAdminUserId(Long adminUserId) {
        PartnerQuery query = new PartnerQuery();
        query.setAdminUserId(adminUserId);
        final Example example = this.buildExample(query);
        example.orderBy("createTime").desc();
        return this.selectFirstOneByExample(example);
    }
}
